﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoldPrinter;
using KeaoBase;
using System.Diagnostics;

namespace KeaoMDS.Report
{
    public partial class YueJieBaoBiao : BaseForm
    {
        private string m_ChengZhongLeiXing = string.Empty;
        public YueJieBaoBiao(string ChengZhongLeiXing)
        {
            InitializeComponent();
            m_ChengZhongLeiXing = ChengZhongLeiXing;
            this.Text = string.Format("{0}月报表", m_ChengZhongLeiXing);
        }

        private void RiJieBaoBiao_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = DateTime.Today.Month-2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //预览
            BindInfo(sender);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //打印
            BindInfo(sender);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //导出
            BindInfo(sender);
        }

        private void BindInfo(object sender)
        {
            ExcelAccess excelAccess = new ExcelAccess();
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Today.ToString("yyyy"),comboBox1.SelectedIndex,KeaoUtils.ConfigGetNodeAttr("时段","月结")));
                DateTime jieShu = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Today.ToString("yyyy"), comboBox1.SelectedIndex + 1, KeaoUtils.ConfigGetNodeAttr("时段", "月结")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                createCondition += KeaoUtils._DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "danweimingcheng,wuliaomingcheng,xinghao,count(id) as cheshu,sum(jingzhong) as jingzhong,sum(kouzhong) as kouzhong,sum(fahuozhongliang) as fahuozhongliang", null, createCondition, "danweimingcheng,wuliaomingcheng,xinghao", null);
                if (Dt.Rows.Count == 0)
                {
                    KeaoUtils.ShowInfoMessage("没有数据。");
                    return;
                }
                excelAccess.Open(Application.StartupPath + "\\Template\\月结报表.xls");
                excelAccess.SetCellText(1, "A", string.Format("{0}月结报表", m_ChengZhongLeiXing));
                excelAccess.SetCellText(2, "A", string.Format("月份：{0}", comboBox1.Text));
                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["danweimingcheng"]));
                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                }

                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", KeaoUtils._CurrentUserRow["yonghuxingming"], DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                Button b = sender as Button;
                if (b.Text == "预览")
                {
                    excelAccess.PrintPreview();
                }
                if (b.Text == "打印")
                {
                    excelAccess.Print();
                }
                if (b.Text == "导出")
                {
                    FolderBrowserDialog fbd = new FolderBrowserDialog();
                    if (fbd.ShowDialog() == DialogResult.OK)
                    {
                        excelAccess.SaveAs(fbd.SelectedPath + string.Format("\\{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss")),true);
                        KeaoUtils.ShowInfoMessage("导出成功。");
                        Process.Start(fbd.SelectedPath);
                    }
                }

            }
            catch(Exception err)
            {
                KeaoUtils.LogTxt(string.Format("导出月结报表异常。{0}", err.Message));
            }
            finally
            {
                excelAccess.Close();
            }
        }
    }
}
